//
//  CZVisitorView.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/16.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit

import SnapKit

//设置代理

@objc protocol CZVisitorViewDelegate: NSObjectProtocol {
    
    func didClickRegisterButton()
    
    func didClickLoginButton()
    
    
    optional func optionalButton()
    
    
    
    
}

class CZVisitorView: UIView {
    
    
    //设置一个代理
    //这里可选我们不知道谁是他的代理
    weak var delegate: CZVisitorViewDelegate?
    
    
    
    //设置登录界面
    
    
    /**
    
    1.设置2个imageView,一个label,2个button,一个半透明的imageView没了
    
    
    
    */
    
    //在init里面添加对应的空间
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加空间
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        setupUI()
        
        
        
        
    }
    
    //提供一个方法让外界访问
    
    func setuserInfo(imageName: String?,text: String?){
        
        //直接默认设置为隐藏
        
        circleView.hidden = true
        
        
        //提示我们需要解包
        
        if let i = imageName, t = text {
            
            
            
            
            iconImageView.image = UIImage(named: i)
            
            contentLabel.text = t
            
        }else{
            
            
            
            //如果有值,那么说明是主界面
            
            startRorate()
            
            
            
            
        }
        
        
        
        
        
        
    }
    
    
    
    
    //开始旋转
    func startRorate(){
        
        circleView.hidden = false
        //这里使用coreAnimation
//        transform.rotation
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        basic.toValue = M_PI * 2
        
        
        //设置动画间隔
        basic.duration = 20
        
        
        //设置重复的次数
        
        basic.repeatCount = MAXFLOAT
        
        //设置他切换界面的时候,不移除掉,除非app被销毁或者退出
        
        basic.removedOnCompletion = false
        
        
        
        //将动画添加到控件上面去
        
        //注意这个key使我们以后可以根据key获取对应的动画对象的
        circleView.layer.addAnimation(basic, forKey: nil)
        
 
        
        
        
        
        
        
        
    }
    
//    //旋转10度
//    
//    func rorate(){
//        
//        circleView.transform = CGAffineTransformRotate(circleView.transform, 0.1)
    
        
//    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        //这个主意添加顺序,我们要把圆形view遮住,单数房子的view不遮住,所以应该设在他们之间
        
        addSubview(circleView)
        
        addSubview(maskImageView)
        
        addSubview(iconImageView)
        
        
        addSubview(contentLabel)
        
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        //设置约束,利用系统的进行设置约束
        
        //添加约束,系统源生的约束
        
//首先我们要关闭autoresizing这个属性
        
        //设置约束改为第三方框架,
        
//        iconImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        
//        //注意这里的self 就是这个view本身
//        
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        //注意空间的约束的顺序不是特别的严格.
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            
            make.centerY.equalTo(self)
            
            
        }
        
        
        //设置圆圈
//        
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterY, multiplier: 1, constant: 0))
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView)
            
            make.centerY.equalTo(iconImageView)
            
            
        }
        
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            
            make.centerY.equalTo(circleView)
            
            
        }
        
      
        
        //设置label的约束
        
//        contentLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: contentLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: contentLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 15))
        
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            
            make.top.equalTo(circleView.snp_bottom)
            
            
        }
        
        
        //设置按钮的约束
        
//        registerButton.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: contentLabel, attribute: .Leading, multiplier: 1, constant: 0))
//        
//        //设置上边
//        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: contentLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel.snp_leading)
            
            make.top.equalTo(contentLabel.snp_bottom)
            
            make.width.equalTo(100)
            
        }
        
        
        //设置宽度
        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//        
        
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: contentLabel, attribute: .Trailing, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(contentLabel.snp_trailing)
            
            make.top.equalTo(contentLabel.snp_bottom)
            
            make.width.equalTo(100)
            
        }
        
        
        
        //设置遮罩的imageview
        
        
        
        //注意这个约束一定要最后设置不然没有用,遮罩显示不出来
//        maskImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
//        
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    //定义房子的图片view
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //定义圆形的图片view
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //定义label
    
    //注意要把按钮给定住,不然会跟着移动
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16, preferredMaxLayoutWidth: 215)
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        
        return label
        
        
    }()
    
    
    
    //注册按钮
    
    private lazy var registerButton: UIButton = {
        
        
        let b = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        
        b.addTarget(self, action: "didClickRegisterButton", forControlEvents: .TouchUpInside)
        
        b.setTitle("注册", forState: .Normal)
        
//        b .setImage(UIImage(named: ""), forState: .Normal)
        
        b.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        
        
        return b
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let b = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        b.addTarget(self, action: "didClickLoginButton", forControlEvents: .TouchUpInside)
        
        b.setTitle("登录", forState: .Normal)
        
        b.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return b
    }()
    
    
    
    //点击按钮时间,监听方法
    
   @objc private func didClickRegisterButton(){
        
    
    //我们以前判断是否有代理
    
    delegate?.didClickRegisterButton()
    
        
        
    }
    
    
   @objc private func didClickLoginButton(){
        
        delegate?.didClickLoginButton()
        
        
    }
    
    
    
    //设置半透明背景
    
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    
    
    
    

    
    
    
    

}
